package org.zhadoop.streaming.util.datatype;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zhadoop.streaming.exception.StreamingException;

/**
 * Boolean 数据类型解析
 * @author zwd
 *
 */
public class BooleanParser implements DataTypeParser {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6390384322932339622L;
	
	private static final Logger LOG = LoggerFactory.getLogger(BooleanParser.class);

	/**
	 * 常量true
	 */
	private static final String CONST_TRUE = "true";
	
	/**
	 * 常量false
	 */
	private static final String CONST_FALSE = "false";
	
	@Override
	public Object createValue(String value) throws StreamingException {
		if(value == null){
			return null;
		}
		
		if(CONST_TRUE.equalsIgnoreCase(value)){
			return true;
		}
		else if(CONST_FALSE.equalsIgnoreCase(value)){
			return false;
		}else{
			LOG.debug("Data not in the boolean data type range so converted to null.");
			return null;
		}
		
	}

	@Override
	public String toStringValue(Object value) throws StreamingException {
		if(null == value){
			return null;
		}
		return value.toString();
	}

}
